//
//  TagTableViewCell.m
//  XPhoto
//
//  Created by sentinel on 2020/4/16.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "TagTableViewCell.h"


@interface TagTableViewCell()
@property(nonatomic, strong) NSMutableArray *tagButtons;
@end
@implementation TagTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)resizeToFit{
    DLOG(@"%@",[self superview]);
    CGRect itemFrame = CGRectZero;
    itemFrame.origin = CGPointMake([self detailWidthWidthCellWidth:self.bounds.size.width], 0);
    for (NSInteger i = 0, n = self.tagButtons.count; i < n; i++) {
        UIButton *btn = [self.tagButtons objectAtIndex:i];
        itemFrame = [self itemFrameWithTag:[btn titleForState:UIControlStateNormal] atPoint:itemFrame.origin cellWidth:self.bounds.size.width];
        itemFrame.origin.x -= 3;
        DLOG(@"itemFrame:%@", NSStringFromCGRect(itemFrame));
        btn.frame = itemFrame;
    }
    NSInteger h = itemFrame.origin.y + itemFrame.size.height;
    DLOG(@"%@,%ld", NSStringFromCGRect(itemFrame), h);
    
//    self.detailViewHeightConstraint.active = NO;
    self.detailViewHeightConstraint.constant = itemFrame.origin.y + itemFrame.size.height;//
//    self.detailViewHeightConstraint.active = YES;
}
- (CGFloat)detailWidthWidthCellWidth:(CGFloat)cellWith{
    return self.detailView.bounds.size.width;
}
- (CGRect)itemFrameWithTag:(NSString *)tag atPoint:(CGPoint)p cellWidth:(CGFloat)width{
    CGRect itemFrame = CGRectZero;
    CGFloat detailViewWidth = [self detailWidthWidthCellWidth:width];
    
    itemFrame.size = [tag sizeWithAttributes:@{NSFontAttributeName:THEME_TAG_BTN_TITLE_FONT}];
    itemFrame.size.width = MIN(itemFrame.size.width + 20, detailViewWidth);
    itemFrame.size.height += 4;
    if ( (p.x - itemFrame.size.width) < 0 ) {
        p = CGPointMake(detailViewWidth, p.y + itemFrame.size.height +3);
    }
    itemFrame.origin = CGPointMake(p.x - itemFrame.size.width, p.y);
    return itemFrame;
}
- (void)setTags:(NSArray<NSString *> *)tags{
    DLOG(@"****************");
    _tags = tags;
    if(self.tagButtons){
        [self.tagButtons makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    NSMutableArray *buttons = [NSMutableArray array];
    CGRect itemFrame = CGRectZero;
    for (NSInteger i = 0, n = tags.count; i < n; i++) {
        NSString *tag = [tags objectAtIndex:i];
        itemFrame = [self itemFrameWithTag:tag atPoint:itemFrame.origin cellWidth:self.bounds.size.width];
        UIButton *btn = [[UIButton alloc] initWithFrame:itemFrame];
        [btn setTitle:tag forState:UIControlStateNormal];
        btn.titleLabel.font = THEME_TAG_BTN_TITLE_FONT;
        [btn setTitleColor:THEME_TAG_BTN_TITLE_COLOR forState:UIControlStateNormal];
        btn.backgroundColor = THEME_TAG_BTN_BG_COLOR;
        btn.clipsToBounds = YES;
        btn.layer.cornerRadius = itemFrame.size.height/2;
        btn.layer.borderWidth = 1.0f;
        btn.layer.borderColor = [THEME_TAG_BTN_BORDER_COLOR CGColor];
        btn.userInteractionEnabled = NO;
        btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        
        [self.detailView addSubview:btn];
        [buttons addObject:btn];
        
        itemFrame.origin.x += itemFrame.size.width + 3;
    }
    self.tagButtons = buttons;
    [self resizeToFit];
}
@end
